package zjhr.com.controller;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import zjhr.com.pojo.EasyUIPageQuery;
import zjhr.com.pojo.ZjFactory;
import zjhr.com.service.FactoryService;
import zjhr.com.utils.JsonUtils;

@Controller
public class FactoryController {
	@Autowired
	private FactoryService factoryService;
	//添加新的公司信息
	@RequestMapping(value="/addFactoryInfo.action", method=RequestMethod.POST)
	public String addFactory(ZjFactory factory){
		factoryService.addFactory(factory);
		return "redirect:/base/qiye.action";
	}
	
	//ajax请求查询id和name
	@RequestMapping(value="/selectFactory.action", method=RequestMethod.POST)
	@ResponseBody
	public String selectFactoryID(){
		List<ZjFactory> list = factoryService.selectIdAndName();
		
		return JsonUtils.objectToJson(list);
	}
	//根据id查询公司信息
	@RequestMapping(value="/findNameShort.action", method=RequestMethod.POST)
	@ResponseBody
	public String findNameShort(String id){
		ZjFactory factory = factoryService.findNameShort(id);
		return JsonUtils.objectToJson(factory);
	}
	//分页
	@RequestMapping("/factoryPageQuery.action")
	@ResponseBody
	public String factoryPageQuery(Integer page,Integer rows){
		EasyUIPageQuery pageQuery = factoryService.pageQuery(page, rows);
		return JsonUtils.objectToJson(pageQuery);
		
	}
	
	//修改信息
	
	@RequestMapping(value="/editFactoryInfo.action", method=RequestMethod.POST)
	public String edit(ZjFactory factory){
		factoryService.edit(factory);
		return "redirect:/base/qiye.action";
	}
	
	//poi
	@RequestMapping("/factoryImport.action")
	public String factoryImport(MultipartFile myFile) throws Exception{
		if(myFile !=null && !"".equals(myFile.getOriginalFilename()) && myFile.getSize()>0){
			InputStream inputStream = myFile.getInputStream();
			HSSFWorkbook workbook = new HSSFWorkbook(inputStream);
			HSSFSheet hssfSheet = workbook.getSheetAt(0);
			List<ZjFactory> list = new ArrayList<>();
			for (Row row : hssfSheet) {
				if(row.getRowNum()==0){
					continue;
				}
				row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
				row.getCell(4).setCellType(Cell.CELL_TYPE_STRING);
				row.getCell(6).setCellType(Cell.CELL_TYPE_STRING);
				String id = row.getCell(0).getStringCellValue();
				String nameFull = row.getCell(1).getStringCellValue();
				String nameShort = row.getCell(2).getStringCellValue();
				String address = row.getCell(3).getStringCellValue();
				String phone = row.getCell(4).getStringCellValue();
				String leader = row.getCell(5).getStringCellValue();
				String leaderPhone = row.getCell(6).getStringCellValue();
				ZjFactory factory = new ZjFactory(id, nameFull, nameShort, address, phone, leader, leaderPhone);
				list.add(factory);
			}
			
			factoryService.addImport(list);
			
			return "redirect:/base/qiye.action";
			
		}else{
			return null;
		}
		
	}

}
